import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/model/course.dart';
import 'package:kxy_flutter_app/model/course_resource.dart';
import 'package:more/tuple.dart';

class CourseDetailRules extends StatelessWidget {
  final Course course;
  const CourseDetailRules({
    Key key,
    @required this.course,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      width: MediaQuery.of(context).size.width,
      height: 320,
      child: Column(
        children: [
          titleWidget(context),
          Expanded(child: contentWidget()),
        ],
      ),
    );
  }

  Widget titleWidget(BuildContext context) {
    return Container(
      alignment: Alignment.center,
      width: MediaQuery.of(context).size.width,
      padding: EdgeInsets.symmetric(vertical: 15),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            color: Color.fromRGBO(240, 240, 240, 1),
            width: 1,
          ),
        ),
      ),
      child: Text(
        '完成规则',
        style: TextStyle(
          color: Colors.black87,
          fontSize: 18,
        ),
      ),
    );
  }

  Widget contentWidget() {
    return ListView(
      children: course.resourceList.map((resource) {
        var rule = getRule(resource);
        return ListTile(
          title: Text(
            rule.first,
            style: TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.bold,
            ),
          ),
          subtitle: Text(
            rule.second,
            style: TextStyle(fontSize: 14),
          ),
        );
      }).toList(),
    );
  }

  Tuple2<String, String> getRule(CourseResource resource) {
    if (resource.type == ResourceType.Picture) {
      return new Tuple2('图文', '${resource.name}学习时长达到${resource.courseTime}秒，为完成。');
    }

    if (resource.type == ResourceType.Video) {
      return new Tuple2('视频', '${resource.name}学习时长达到${resource.courseTime}秒，为完成。');
    }

    if (resource.type == ResourceType.Exam) {
      return new Tuple2('考试', '${resource.name}达到${resource.score}分，为完成。');
    }

    if (resource.type == ResourceType.Document) {
      return new Tuple2('课件', '${resource.name}学习时长达到${resource.courseTime}秒，为完成。');
    }

    return new Tuple2('未知的课件类型', '未知的课件类型，请联系管理员处理。');
  }
}
